package com.example.shootinggamebackend.controller;

import com.example.shootinggamebackend.service.SpeechRecognizeService;
import com.example.shootinggamebackend.service.SpeechSynthesisService;
import com.example.shootinggamebackend.service.TextLLMService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/llm")
public class LLMController {

    @Autowired
    SpeechRecognizeService recognizeService;
    @Autowired
    SpeechSynthesisService synthesisService;
    @Autowired
    TextLLMService textLLMService;

    @PostMapping("/speech/recognize")
    public String recognizeSpeech(@RequestParam String filePath) {
        // 语音识别大模型识别 mp3 文件
        String text = recognizeService.callMultiModalConversation(filePath);
        // 文本大模型识别并提取角度数据
        String indication = textLLMService.callTextLLM("你是一名球员，下面的文本在指挥你踢球。请你根据下面的文本，给出踢球的" +
                "角度的具体数值，一般在60到120之间。\n正北方向、正前方向，都表示90度。正东方向、正右方，都表示0度。\n比如：朝北偏东20度踢球，" +
                "输出格式示范：70。\n具体指示如下，请你给出角度的具体数值：\n" + text);
        System.out.println("文本大模型识别语音转译结果："+indication);
        List<Integer> recognizeRes = recognizeService.extractNumbers(indication);
        for(int i:recognizeRes){
            System.out.println("识别角度："+i);
        }
        if(recognizeRes.isEmpty()){
            return "90";
        } else {
            return String.valueOf(recognizeRes.get(0));
        }
    }

    @PostMapping("/speech/synthesis")
    public void synthesisSpeech(@RequestParam String filePath) {
        synthesisService.callStreamAuidoDataToSpeaker();
    }

    @PostMapping("/text")
    public String generateText(@RequestParam String prompt){
        return textLLMService.callTextLLM(prompt);
    }
}
